package org.dromara.third.convert.gatherPeople.validator;

import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.convert.gatherPeople.dto.GatherPeopleDTO;
import org.dromara.third.convert.util.ExistenceValidator;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class GatherPeopleValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return GatherPeopleDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        GatherPeopleDTO gatherPeopleDTO = (GatherPeopleDTO) target;

        // 基础字段验证
        validateRequiredFields(gatherPeopleDTO, errors);

        // 存在性验证
        validateExistence(gatherPeopleDTO, errors);
    }

    /**
     * 验证必填字段
     */
    private void validateRequiredFields(GatherPeopleDTO dto, Errors errors) {
        ValidateUtil.rejectIfNull(dto::getId, String.valueOf(ExceptionEnum.AuthException01.getCode()), "主键不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getAlarmTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "报警时间不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getAlarmNumber, String.valueOf(ExceptionEnum.AuthException01.getCode()), "报警人数不能为空", errors);
        ValidateUtil.rejectIfNull(dto::getAlarmLevel, String.valueOf(ExceptionEnum.AuthException01.getCode()), "报警等级不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getGatheringAreaName, String.valueOf(ExceptionEnum.AuthException01.getCode()), "聚集区域名称不能为空", errors);
        if (CollectionUtils.isEmpty(dto.getAlarmArea())) {
            errors.reject(String.valueOf(ExceptionEnum.AuthException06.getCode()), "聚集人员列表不能为空");
        } else {
            dto.getAlarmArea().forEach(alarmArea -> {
                ValidateUtil.rejectIfEmpty(alarmArea::getName, String.valueOf(ExceptionEnum.AuthException01.getCode()), "姓名不能为空", errors);
                ValidateUtil.rejectIfEmpty(alarmArea::getLongitude, String.valueOf(ExceptionEnum.AuthException01.getCode()), "经度不能为空", errors);
                ValidateUtil.rejectIfEmpty(alarmArea::getLatitude, String.valueOf(ExceptionEnum.AuthException01.getCode()), "纬度不能为空", errors);
            });
        }
        ValidateUtil.rejectIfEmpty(dto::getDelFlag, String.valueOf(ExceptionEnum.AuthException01.getCode()), "删除标志不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateDept, String.valueOf(ExceptionEnum.AuthException01.getCode()), "所属部门不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getCreateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建时间不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getUpdateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新人不能为空", errors);
        ValidateUtil.rejectIfEmpty(dto::getUpdateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新时间不能为空", errors);
    }

    /**
     * 验证存在性
     */
    private void validateExistence(GatherPeopleDTO dto, Errors errors) {
        // 验证所属部门存在
        ExistenceValidator.validateDeptExists(dto.getCreateDept(), "所属部门", errors);

        // 验证创建人存在
        ExistenceValidator.validateUserExists(dto.getCreateBy(), "创建人", errors);

        // 验证更新人存在
        ExistenceValidator.validateUserExists(dto.getUpdateBy(), "更新人", errors);
    }
}
